/*
    XFlightRec
    Copyright (C) 2009 Mike Gehl

    X-Plane Plugin SDK
    Copyright (C) Sandy Barbour and Ben Supnik

    Data conversion routines to NAV3
    Copyright (C) 2000 Ted Wright
    NASA Business: wright@grc.nasa.gov, Remainder: wright@en.com

    This file is part of XFlightRec.

    XFlightRec is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    XFlightRec is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XFlightRec.  If not, see <http://www.gnu.org/licenses/>.
    ---------------------------------------------------------------------------
    xflightrec.h
    Copyright (C) Mike Gehl 2009 <mjgehl@gmail.com>
*/

#ifndef XFLIGHTREC_H_INCLUDED
#define XFLIGHTREC_H_INCLUDED

#include <inttypes.h>
#include "XPLMDataAccess.h"
#include "XPLMMenus.h"

/* Program Name*/
static const char* PRG_NAME = "XFlightRec";

/* Version Number*/
static const char* PRG_VERSION = "1.0";

/* Copyright */
static const char* PRG_COPYRIGHT = "Copyright (C) Mike Gehl 2009";

/* Number of seconds to delay before writing next flight data record*/
static const float FLTREC_INTERVAL = 1.0;

/* Status of flight recorder. OFF or ON */
enum flight_recorder_status {FLTREC_OFF, FLTREC_ON};

enum flight_recorder_menus {FLTREC_MENU_ON, FLTREC_MENU_OFF, FLTREC_MENU_ABOUT};

#if LIN
/* Data type for compatibility with FLTREC.DLL. */
typedef uint32_t DWORD;

/* Data type for compatibility with FLTREC.DLL. */
typedef uint16_t WORD;
#endif


/**
 * Required: This is a forced API (must be included) by the SDK.
 * It is called once when the plugin is loaded. (constructor)
 *
 * Allocate any permanent resources, do initialization, and register any other
 * callbacks. This is where the user interface is initialized.
 * Also return the plugin's name, signature, and a description to the XPLM.
 *
 * @param outName Name of the plugin
 * @param outSig Plugin signature
 * @param outDesc Plugin Description
 * @return 1 on successful init
 */
PLUGIN_API int XPluginStart(char *, char *, char *);

/**
 * Required: This is a forced API (must be included) by the SDK.
 * It is called once when the plugin is unloaded. (destructor)
 *
 * De-allocate any callbacks, free memory and close any open files.
 */
PLUGIN_API void	XPluginStop(void);

/**
 * Required: This is a forced API (must be included) by the SDK.
 * This is called when the plugin is enabled, either right after it is loaded
 * or when a user decides to enable it via the Plugin Manager.
 *
 * This is called when the plugin is enabled.
 * @return  1 if enable was successful
 */
PLUGIN_API int XPluginEnable(void);

/**
 * Required: This is a forced API (must be included) by the SDK.
 * This is called when the plugin is disabled, either just before it is
 * unloaded or when a user decides to disable it via the Plugin Manager.
 */
PLUGIN_API void XPluginDisable(void);

/**
 * Required: This is a forced API (must be included) by the SDK.
 * Used or not, we must make provision to receive messages from keyboard,
 * mouse, X-Plane and other plugins.
 *
 * Not used in this plugin
 *
 * @param inFromWho
 * @param inMessage
 * @param inParam
 */
PLUGIN_API void XPluginReceiveMessage( XPLMPluginID, long, void *);

/**
 * Determine which XFlightRec menu item was selected and Start or
 * Stop the recorder.
 *
 * @param inMenuRef Void pointer to the XFlightRec menu
 * @param inItemRef Void pointer to the XFlightRec menu item selected
 */
void MyMenuHandlerCallback(void *, void *);

/**
 * Determine which XFlightRec menu item was selected and Start or
 * Stop the recorder.
 *
 * @param inElapsedSinceLastCall
 * @param inElapsedTimeSinceLastFlightLoop
 * @param inCounter
 * @param inRefcon
 *
 * @return Return Time interval to be called again.
 */
float MyFlightLoopCallback(float, float, int,  void *);

/**
 * Stop the Flight Data Recorder. Close any open file
 */
void fltrec_stop(void);

/**
 * Start the Flight Data Recorder. Open and overwrite a previous data file.
 */
void fltrec_start(void);

/* X-Plane Dataref:  sim/time/zulu_time_sec */
XPLMDataRef gZuluTime;

/* X-Plane Dataref:  sim/flightmodel/position/latitude */
XPLMDataRef gPlaneLat;

/* X-Plane Dataref: sim/flightmodel/position/longitude */
XPLMDataRef gPlaneLon;

/* X-Plane Dataref: sim/flightmodel/position/elevation */
XPLMDataRef gPlaneAlt;

/* X-Plane Dataref: sim/flightmodel/position/theta */
XPLMDataRef gPlanePitch;

/* X-Plane Dataref: sim/flightmodel/position/phi */
XPLMDataRef gPlaneRoll;

/* X-Plane Dataref: sim/flightmodel/position/psi */
XPLMDataRef gPlaneHeading;

/* X-Plane Dataref: sim/flightmodel/position/indicated_airspeed */
XPLMDataRef gPlaneIAS;

/* X-Plane Dataref: sim/flightmodel/position/true_airspeed */
XPLMDataRef gPlaneTAS;

/* X-Plane Dataref: sim/flightmodel2/gear/deploy_ratio */
XPLMDataRef gPlaneGear;

/**
 * Flight Data Recorder binary data format
 * See: http://tinyurl.com/d3j72u
 *
 * The format of each record is defined by the FLTREC_DATA
 * structure shown below.  Note:  You must set your compiler's structure
 * packing to one or two bytes for the FLTREC_DATA structure to be usable.  A
 * WORD is a 16-bit unsigned integer.  A DWORD is a 32-bit unsigned integer.
 */
typedef struct tagFLTREC_DATA
{
  // FLTREC.DLL version
  WORD   wVersion1;   // 6
  WORD   wVersion2;   // 0
  WORD   wVersion3;   // 1
  WORD   wVersion4;   // build id

  // time of day
  WORD   wClockHour;  // hour
  WORD   wClockMin;   // minute
  WORD   wClockSec;   // second

  // position
  DWORD  dwLatLo;     // latitude  (low dword)
  DWORD  dwLatHi;     // latitude  (high dword)
  DWORD  dwLonLo;     // longitude (low dword)
  DWORD  dwLonHi;     // longitude (high dword)
  DWORD  dwAltLo;     // altitude  (low dword)
  DWORD  dwAltHi;     // altitude  (high dword)

  // orientation
  // angles: 180 deg == 0x8000
  DWORD  dwPitch;      // pitch
  DWORD  dwBank;       // bank
  DWORD  dwHeading;    // heading

  // airspeed
  // units: 256 * meters/second
  DWORD  dwTAS;        // true
  DWORD  dwIAS;        // indicated

  // fuel tank quantities
  // percentages: 100% == 0x800000
  DWORD  dwFuelLeftPct;     // left
  DWORD  dwFuelLeftAuxPct;  // left aux
  DWORD  dwFuelRightPct;    // right
  DWORD  dwFuelRightAuxPct; // right aux

  // gear position
  // up == 0, down == 0xFFFF
  WORD   wGear1;
  WORD   wGear2;
  WORD   wGear3;

} __attribute__((__packed__)) FLTREC_DATA;


#endif // XFLIGHTREC_H_INCLUDED


